package com.jeeplus.mob.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{
  public static final int DEFAULT = 0;
  public static final int YM = 1;
  public static final int YMR_SLASH = 11;
  public static final int NO_SLASH = 2;
  public static final int YM_NO_SLASH = 3;
  public static final int DATE_TIME = 4;
  public static final int DATE_TIME_NO_SLASH = 5;
  public static final int DATE_HM = 6;
  public static final int TIME = 7;
  public static final int HM = 8;
  public static final int LONG_TIME = 9;
  public static final int SHORT_TIME = 10;
  public static final int DATE_TIME_LINE = 12;
  
  
  public static Date dateRoll(Date date, int i, int d) {
      // 获取Calendar对象并以传进来的时间为准
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      // 将现在的时间滚动固定时长,转换为Date类型赋值
      calendar.add(i, d);
      // 转换为Date类型再赋值
      date = calendar.getTime();
      return date;
  }
  
  //时间戳转日期
  public static String timeStampToStrs(int number) {
	  
	  Date date = new Date(number);
	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  return sdf.format(date);
  }
//时间戳转日期
  public static String timeStampToStr(int number) {
	  
	  Date date = new Date(number);
	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	  return sdf.format(date);
  }
  
  
  
  public static String dateToStr(Date date, String pattern)
  {
    if ((date == null) || (date.equals(""))) {
      return null;
    }
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    return formatter.format(date);
  }
  
  public static String dateToStr(Date date)
  {
    return dateToStr(date, "yyyy/MM/dd");
  }
  
  public static String dateToStr(Date date, int type)
  {
    switch (type)
    {
    case 0: 
      return dateToStr(date);
    case 1: 
      return dateToStr(date, "yyyy/MM");
    case 2: 
      return dateToStr(date, "yyyyMMdd");
    case 11: 
      return dateToStr(date, "yyyy-MM-dd");
    case 3: 
      return dateToStr(date, "yyyyMM");
    case 4: 
      return dateToStr(date, "yyyy/MM/dd HH:mm:ss");
    case 5: 
      return dateToStr(date, "yyyyMMddHHmmss");
    case 6: 
      return dateToStr(date, "yyyy/MM/dd HH:mm");
    case 7: 
      return dateToStr(date, "HH:mm:ss");
    case 8: 
      return dateToStr(date, "HH:mm");
    case 9: 
      return dateToStr(date, "HHmmss");
    case 10: 
      return dateToStr(date, "HHmm");
    case 12: 
      return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
    case 13: 
      return dateToStr(date, "MMdd");
    }
    throw new IllegalArgumentException("Type undefined : " + type);
  }
  
  public static Date StrToDate(String Strdate, String pattern)
  {
    if ((Strdate == null) || (Strdate.equals(""))) {
      return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Date date = null;
    try
    {
      date = sdf.parse(Strdate);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
    return date;
  }
  
  public static Date StrToDate(String date, int type)
  {
    switch (type)
    {
    case 1: 
      return StrToDate(date, "yyyy/MM");
    case 2: 
      return StrToDate(date, "yyyyMMdd");
    case 11: 
      return StrToDate(date, "yyyy-MM-dd");
    case 3: 
      return StrToDate(date, "yyyyMM");
    case 4: 
      return StrToDate(date, "yyyy/MM/dd HH:mm:ss");
    case 5: 
      return StrToDate(date, "yyyyMMddHHmmss");
    case 6: 
      return StrToDate(date, "yyyy/MM/dd HH:mm");
    case 7: 
      return StrToDate(date, "HH:mm:ss");
    case 8: 
      return StrToDate(date, "HH:mm");
    case 9: 
      return StrToDate(date, "HHmmss");
    case 10: 
      return StrToDate(date, "HHmm");
    case 12: 
      return StrToDate(date, "yyyy-MM-dd HH:mm:ss");
    }
    throw new IllegalArgumentException("Type undefined : " + type);
  }
  
  public static Date getFirstDay(int year, int month) {
		// 获取Calendar类的实例
		Calendar c = Calendar.getInstance();
		// 设置年份
		c.set(Calendar.YEAR, year);
		// 设置月份，因为月份从0开始，所以用month - 1
		c.set(Calendar.MONTH, month - 1);
		// 设置日期
		c.set(Calendar.DAY_OF_MONTH, 1);

		return c.getTime();
	}

	public static Date getLastDay(int year, int month) {
		// 获取Calendar类的实例
		Calendar c = Calendar.getInstance();
		// 设置年份
		c.set(Calendar.YEAR, year);
		// 设置月份，因为月份从0开始，所以用month - 1
		c.set(Calendar.MONTH, month - 1);
		// 获取当前时间下，该月的最大日期的数字
		int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 将获取的最大日期数设置为Calendar实例的日期数
		c.set(Calendar.DAY_OF_MONTH, lastDay);

		return c.getTime();
	}
}

